﻿@page "/SysClient/Edit/{id}"
@using SelfService.ViewModel.SysClientVMs;
@inherits BasePage

<ValidateForm @ref="vform" Model="@Model" OnValidSubmit="@Submit">
    <Row ItemsPerRow="ItemsPerRow.Two" RowType="RowType.Normal">
        <Select @bind-Value="@Model.Entity.UserId"
                Items="@AllUsers"
                PlaceHolder="@WtmBlazor.Localizer["Sys.PleaseSelect"]">
        </Select>
        <BootstrapInput @bind-Value="@Model.Entity.Ip"  />
        <BootstrapInput @bind-Value="@Model.Entity.MacAddress"  />
        <BootstrapInput @bind-Value="@Model.Entity.Version"  />
        <Switch @bind-Value="@Model.Entity.IsUpdate"  />
        <Select
            @bind-Value="@Model.Entity.ThemeId"
            Items="@AllSysThemes"
            PlaceHolder="@WtmBlazor.Localizer["Sys.PleaseSelect"]">
        </Select>
        <Select @bind-Value="@Model.Entity.UserId"
                Items="@AllUsers"
                PlaceHolder="@WtmBlazor.Localizer["Sys.PleaseSelect"]">
        </Select>
        <Row ColSpan="2">
            <Transfer @bind-Value="@Model.SelectedModulesIDs" Items="@AllSysModules"/>
        </Row>
        <Row ColSpan="2">
            <Transfer @bind-Value="@Model.SelectedDevicesIDs" Items="@AllSysDevices"/>
        </Row>
        <Row ColSpan="2">
            <Transfer @bind-Value="@Model.SelectedCardTypesIds" Items="@AllSysCardTypes"/>
        </Row>
        <Row ColSpan="2">
            <Transfer @bind-Value="@Model.SelectedPayTypesIds" Items="@AllSysPayTypes"/>
        </Row>
        <Row ColSpan="2">
            <Transfer @bind-Value="@Model.SelectedRoomsIds" Items="@AllSysRooms"/>
        </Row>

    </Row>
    <div class="modal-footer table-modal-footer">
        <Button Color="Color.Warning" Icon="fa fa-close" Text="@WtmBlazor.Localizer["Sys.Close"]" OnClick="OnClose"></Button>
        <Button Color="Color.Primary" ButtonType="ButtonType.Submit" Icon="fa fa-save" Text="@WtmBlazor.Localizer["Sys.Edit"]" IsAsync="true"></Button>
    </div>
</ValidateForm>

@code {

    private SysClientVm Model = null;
    private ValidateForm vform { get; set; }
    [Parameter]
    public string id { get; set; }

    private IEnumerable<SelectedItem> AllSysModules { get; set; }

    private IEnumerable<SelectedItem> AllSysDevices { get; set; }

    private IEnumerable<SelectedItem> AllSysCardTypes { get; set; }

    private IEnumerable<SelectedItem> AllSysPayTypes{ get; set; }

    private IEnumerable<SelectedItem> AllSysRooms { get; set; }

    private IEnumerable<SelectedItem> AllSysThemes { get; set; }

    private IEnumerable<SelectedItem> AllUsers { get; set; }
    
    protected override async Task OnInitializedAsync()
    {
        AllSysModules = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysModules");
        AllSysDevices = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysDevices");
        AllSysCardTypes = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysCardTypes");
        AllSysPayTypes = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysPayTypes");
        AllSysThemes = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysThemes", placeholder: "请选择 ...");
        AllSysRooms = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysRooms");
        AllUsers = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetUsers", placeholder: "请选择 ...");
        var rv = await WtmBlazor.Api.CallAPI<SysClientVm>($"/api/SysClient/{id}");
        Model = rv.Data;
        await base.OnInitializedAsync();
    }

    private async Task Submit(EditContext context)
    {
        await PostsForm(vform, $"/api/SysClient/edit", (s) => "Sys.OprationSuccess", method: HttpMethodEnum.PUT);
    }

    public void OnClose()
    {
        CloseDialog();
    }
}
